const mysql = require('mysql2/promise');
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env') });

const pool = mysql.createPool({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 3306,
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0,
  // ensure driver uses China Standard Time (UTC+8) for TIMESTAMP/DATETIME handling
  timezone: '+08:00'
});
console.log('Connected to MySQL database');
console.log('DB_USER:'+process.env.DB_USER);
console.log('DB_PORT:'+process.env.DB_PORT);
console.log('DB_PASS:'+process.env.DB_PASS);
console.log('DB_NAME:'+process.env.DB_NAME);
console.log('DB_HOST:'+process.env.DB_HOST);

module.exports = pool;